package com.example.shaoying.yuejian.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.shaoying.yuejian.R;
import com.example.shaoying.yuejian.activity.TrainJoinActivity;
import com.example.shaoying.yuejian.adapter.AskAdapter;
import com.example.shaoying.yuejian.javabean.AskList;
import com.example.shaoying.yuejian.javabean.VideoList;
import com.example.shaoying.yuejian.model.ActivityModel;

import java.net.IDN;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class problemFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    private Context context;
    private View view;
    private RecyclerView recyc;
     private List<AskList> list;
    private String Id,content;
    private EditText Eask_content;
    private Button ask_submit;
    private String userId;//为用户id
    private SharedPreferences sp;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.teacher_wenti, container, false);
        init();
        conn();
        ask_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String str= Eask_content.getText().toString();
                ActivityModel model=new ActivityModel();
                Call<Integer> call=model.problem(Id,userId,str);
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                        Integer res=response.body();
                        if(res>0){

                            Toast.makeText(getActivity(), res+"提问成功", Toast.LENGTH_SHORT).show();
                            conn();
                        }
                    }

                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {

                    }
                });
            }
        });
        return view;
    }

    private void conn() {
        ActivityModel model=new ActivityModel();
        Call<List<AskList>> call=model.getAskList(Id);
        call.enqueue(new Callback<List<AskList>>() {
            @Override
            public void onResponse(Call<List<AskList>> call, Response<List<AskList>> response) {
                list=response.body();
                AskAdapter a=new AskAdapter(list,context);
                LinearLayoutManager linear=new LinearLayoutManager(context);
                linear.setOrientation(LinearLayoutManager.VERTICAL);
                recyc.setLayoutManager(linear);
                recyc.setItemAnimator(new DefaultItemAnimator());
                recyc.setAdapter(a);
            }

            @Override
            public void onFailure(Call<List<AskList>> call, Throwable t) {
                Toast.makeText(context,"留言列表获取失败",Toast.LENGTH_SHORT).show();
            }
        });



    }

    private void init() {
        context=getActivity();
        Intent intent=getActivity().getIntent();
        Id=intent.getStringExtra("tea_id");
        recyc=(RecyclerView)view.findViewById(R.id.re_ask);
        Eask_content= (EditText) view.findViewById(R.id.ask_content);
        ask_submit= (Button) view.findViewById(R.id.ask_submit);
        sp =getActivity().getSharedPreferences("login_uid", Context.MODE_PRIVATE);
        userId=sp.getString("userId","");
    }


}
